#ifndef BINTOSPI_H
#define BINTOSPI_H

#include "include.h"
#include "File.h"

#define PACKET_MAX_SZIE   64
#define BYTE_LEN   92
#define		SPI_CPHA   				0x01
#define     SPI_BUFFER_OTA          0x320    //!< OTA APPLICATION

 ////////////////////////////////////////////////////////////
 //! @name messagetypes
 //! Types for SPI messages. Can be combined for e.g. NORM | BEGIN | END
 //! @{
#define    SPI_MSGTYPE_NORM     	   ( 0 << 0)    //!< normal
#define    SPI_MSGTYPE_CRIT            ( 1 << 0)    //!< critical
#define    SPI_MSGTYPE_FAIL            ( 1 << 1)    //!< failure
#define    SPI_MSGTYPE_BGN             ( 1 << 2)    //!< begin
#define    SPI_MSGTYPE_END             ( 1 << 3)    //!< end
#define    SPI_MSGTYPE_BINA            ( 1 << 4)    //!< binary for slot A
#define    SPI_MSGTYPE_BINB            ( 1 << 5)    //!< binary for slot B


typedef struct APPApartInfo {
	uint8_t  Data[PACKET_MAX_SZIE];
	uint8_t	 ValidSizeLength;	//max Length is 64 bytes.
}APPInfo_t;

typedef struct SPI_PacketInfo {
	uint32_t TotalPacket;
	uint32_t CurrentBasePacket;
	uint8_t  PacketData[PACKET_MAX_SZIE];
	uint8_t	 PacketValidSizeLength;	//max Length is 64 bytes.
	char 	*slot;
}OTA_SinglePacketInfo_t;

typedef struct {
	uint16_t  size;                    //!< size of the whole struct
	uint16_t  bufftype:12;            //!< type of the BUFFER, SPI_BUFFER_OTA
	uint16_t  hBus:4;

	uint32_t  time[2];                //!< timestamp of message
/**
 * @tag[0]: It used to store the TotalPacket
 * @tag[1]: It used to store the CurrentBasePacket.
*/
	uint32_t  tag[2];                    //!< a tag field for user e.g. object pointer

	uint8_t   res1;                    //!< reserved for user e.g. available data size
	uint8_t   crc8;                    //!< crc8 of message
	uint16_t  msgtype;                //!< type of message, bitfield, see SPI_MSGTYPE_...

	uint32_t  id;                        //!< identifier of message

	uint8_t   data8[64];            //!< data of message (byte based)

} OTASPIMsg_t;

static const char *device = "/dev/spidev0.0";
static uint8_t bits = 8;
static uint32_t speed = 1000000;
static uint32_t mode;
/**************************Functions******************************/
extern int SPISet(const char* spi_device);
//返回文件的总包数
extern unsigned int OTATotalPacketNum(unsigned int fileLength);
extern void BinFileConvertToSP(const BinFileInfo BinFile,FrameInfo_t index, OTA_SinglePacketInfo_t* SP);
extern void FillSingleFrame(const OTA_SinglePacketInfo_t* SP, OTASPIMsg_t* spi_frame);
#endif // !BINTOSPI_H
